package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/8/9.<br><br>
 *
 * 简单：反转字符串Ⅱ
 */
public class Topic541 {
    public static void main(String[] args) {
        Topic541 instance = new Topic541();

        System.out.println(instance.reverseStr("abcdefg", 2));
        System.out.println(instance.reverseStr("abcd", 2));
        System.out.println(instance.reverseStr("a", 2));
    }

    public String reverseStr(String s, int k) {
        char[] result = new char[s.length()];

        boolean reverse = true;
        int startIndex = 0;
        char[] chars = s.toCharArray();
        int i = 0;
        while (i < chars.length) {
            int tempIndex = k - 1;
            while (i < chars.length && tempIndex >= 0) {
                if(reverse) {
                    while (startIndex + tempIndex >= chars.length) {
                        tempIndex --;
                    }
                    result[startIndex + tempIndex] = chars[i];
                }
                else {
                    result[i] = chars[i];
                }

                tempIndex--;
                i++;
            }

            startIndex += k;
            reverse = !reverse;
        }

        return String.valueOf(result);
    }
}
